<?php

/*
* StiPHPcms, A web Content management System made with PHP/MySQL
* JordSti : jord52@gmail.com
* Version : 0.0.2
*
* Copyright (C) 2009  jord52@gmail.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


function loadfile($path){
	$fp = fopen($path,'r');
	$file = "";
	while($line = fgets($fp)){
		$file .= $line;
	}
	fclose($fp);

	return $file;
}

if(!isset($_GET['tomake']))
{
	header('location: index.php');
	exit;
}

$file = loadfile("src/".$_GET['tomake']);

preg_match("#\#name = (.+);#isU",$file,$matches);
$theme_name = $matches[1];

preg_match("#<!-- BEGIN -->(.+)<!-- END -->#isU",$file,$matches);
$stm = $matches[1];

preg_match("#<!-- CSS BEGIN -->(.+)<!-- CSS END -->#isU",$stm,$matches);
$css = $matches[1];

preg_match("#<!-- ERRORPAGE BEGIN -->(.+)<!-- ERRORPAGE END -->#isU",$stm,$matches);
$errorpage = $matches[1];

preg_match("#<!-- HEADER START -->(.+)<!-- HEADER END -->#isU",$stm,$matches);
$header = $matches[1];

preg_match("#<!-- NAVBARLINK START -->(.+)<!-- NAVBARLINK END -->#isU",$header,$matches);
$navbarlink = $matches[1];

preg_match("#<!-- MENUSEC START -->(.+)<!-- MENUSEC END -->#isU",$stm,$matches);
$menu = $matches[1];

preg_match("#<!-- CONTENT START -->(.+)<!-- CONTENT END -->#isU",$stm,$matches);
$content = $matches[1];

preg_match("#<!-- FOOTER START -->(.+)<!-- FOOTER END -->#isU",$stm,$matches);
$footer = $matches[1];

preg_match("#<!-- FRAME START -->(.+)<!-- FRAME END -->#isU",$content,$matches);
$frame = $matches[1];

preg_match("#<!-- MENUITEM START -->(.+)<!-- MENUITEM END -->#isU",$menu,$matches);
$menuitem = $matches[1];

preg_match("#<!-- MENUTAB START -->(.+)<!-- MENUTAB END -->#isU",$menu,$matches);
$menutab = $matches[1];

preg_match("#<!-- WELCOME START -->(.+)<!-- WELCOME END -->#isU",$content,$matches);
$welcome = $matches[1];

$header = preg_replace("#<!-- NAVBARLINK START -->(.+)<!-- NAVBARLINK END -->#isU","<!-- NAVBARLINK MARKER -->",$header);

$content = preg_replace("#<!-- FRAME START -->(.+)<!-- FRAME END -->#isU","<!-- FRAME MARKER -->",$content);
$content = preg_replace("#<!-- WELCOME START -->(.+)<!-- WELCOME END -->#isU","<!-- WELCOME MARKER -->",$content);

$menu = preg_replace("#<!-- MENUITEM START -->(.+)<!-- MENUITEM END -->#isU","<!-- MENUITEM MARKER -->",$menu);
$menu = preg_replace("#<!-- MENUTAB START -->(.+)<!-- MENUTAB END -->#isU","",$menu);


preg_match("#<!-- MENULIST START -->(.+)<!-- MENULIST END -->#isU",$menu,$matches);
$menulist = $matches[1];


$menu = preg_replace("#<!-- MENULIST START -->(.+)<!-- MENULIST END -->#isU","<!-- MENULIST MARKER -->",$menu);

mkdir("tmp/".$theme_name);
chmod("tmp/".$theme_name,0777);

$fp = fopen("tmp/".$theme_name."/css.stt",'w');
fwrite($fp,$css);
fclose($fp);
chmod("tmp/".$theme_name."/css.stt",0777);

$fp = fopen("tmp/".$theme_name."/errorpage.stt",'w');
fwrite($fp,$errorpage);
fclose($fp);
chmod("tmp/".$theme_name."/errorpage.stt",0777);

$fp = fopen("tmp/".$theme_name."/head.stt",'w');
fwrite($fp,$header);
fclose($fp);
chmod("tmp/".$theme_name."/head.stt",0777);

$fp = fopen("tmp/".$theme_name."/navbarlink.stt",'w');
fwrite($fp,$navbarlink);
fclose($fp);
chmod("tmp/".$theme_name."/navbarlink.stt",0777);

$fp = fopen("tmp/".$theme_name."/menu.stt",'w');
fwrite($fp,$menu);
fclose($fp);
chmod("tmp/".$theme_name."/menu.stt",0777);

$fp = fopen("tmp/".$theme_name."/frame.stt",'w');
fwrite($fp,$frame);
fclose($fp);
chmod("tmp/".$theme_name."/frame.stt",0777);

$fp = fopen("tmp/".$theme_name."/welcome.stt",'w');
fwrite($fp,$welcome);
fclose($fp);
chmod("tmp/".$theme_name."/welcome.stt",0777);

$fp = fopen("tmp/".$theme_name."/menuitem.stt",'w');
fwrite($fp,$menuitem);
fclose($fp);
chmod("tmp/".$theme_name."/menuitem.stt",0777);

$fp = fopen("tmp/".$theme_name."/menutab.stt",'w');
fwrite($fp,$menutab);
fclose($fp);
chmod("tmp/".$theme_name."/menutab.stt",0777);

$fp = fopen("tmp/".$theme_name."/menulist.stt",'w');
fwrite($fp,$menulist);
fclose($fp);
chmod("tmp/".$theme_name."/menulist.stt",0777);

$fp = fopen("tmp/".$theme_name."/content.stt",'w');
fwrite($fp,$content);
fclose($fp);
chmod("tmp/".$theme_name."/content.stt",0777);

$fp = fopen("tmp/".$theme_name."/footer.stt",'w');
fwrite($fp,$footer);
fclose($fp);
chmod("tmp/".$theme_name."/footer.stt",0777);

//Making template file

$stm_out = '<?php
//Sti HTML template File
//Generated by stmkit

$frame = new ContentFrame(\''.loadfile("tmp/".$theme_name."/frame.stt").'\');
$welcome = new ContentWelcome(\''.loadfile("tmp/".$theme_name."/welcome.stt").'\');
$contentHTML = new ContentHTML(\''.loadfile("tmp/".$theme_name."/content.stt").'\');

$menuItem = new MenuItem(\''.loadfile("tmp/".$theme_name."/menuitem.stt").'\');
$menuTab = new MenuTab(\''.loadfile("tmp/".$theme_name."/menutab.stt").'\');
$menuList = new MenuList(\''.loadfile("tmp/".$theme_name."/menulist.stt").'\');
$menuHTML = new MenuHTML(\''.loadfile("tmp/".$theme_name."/menu.stt").'\');

$navBarLink = new NavBarLink(\''.loadfile("tmp/".$theme_name."/navbarlink.stt").'\');
$headHTML = new HeadHTML(\''.loadfile("tmp/".$theme_name."/head.stt").'\');

$templateFooter = new TemplateFooter(\''.loadfile("tmp/".$theme_name."/footer.stt").'\');

$templateMenu = new TemplateMenu($menuItem,$menuTab,$menuList,$menuHTML);
$templateContent = new TemplateContent($contentHTML,$frame,$welcome);
$templateHead = new TemplateHead($headHTML,$navBarLink);

$templateError = new TemplateError(\''.loadfile("tmp/".$theme_name."/errorpage.stt").'\');

$template = new Template(\''.$theme_name.'\',$templateHead,$templateMenu,$templateContent,$templateFooter,$templateError);

?>';

$fp = fopen("tmp/stmout/".$theme_name.".stm",'w');
fwrite($fp,$stm_out);
fclose($fp);
chmod("tmp/stmout/".$theme_name.".stm",0777);

copy("tmp/stmout/".$theme_name.".stm","../themes/".$theme_name.".stm");
copy("tmp/".$theme_name."/css.stt","../themes/".$theme_name.".css");

echo "Theme builded !";
?>